package dao;

import entity.Calendar;
import entity.IEntity;
import entity.Student;
import entity.Subject;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author Alexander
 */
public class SubjectDAO implements IDAO {

    @Override
    public void persist(IEntity entity, EntityManager em) {
        em.persist(entity);
    }

    @Override
    public IEntity find(Object id, EntityManager em) {
        return em.find(Subject.class, (int) id);
    }

    @Override
    public void update(IEntity entity, EntityManager em) {
        em.merge(entity);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        Subject materia = em.getReference(Subject.class, (int) id);
        em.remove(materia);
    }
    
    public List<String> subjectsNames(Integer calendar,EntityManager em){
        List<String> subName = new ArrayList<>();
        
        Query q = em.createQuery("SELECT name FROM Subject m "
                + "WHERE m.calendario :=calendar");
        try {
            subName = q.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return subName;
    }

    public List<Subject> search(Object guess, boolean byCode, EntityManager em) {
        List<Subject> materias = new ArrayList<>();
        Query q;
        if (byCode) {
            q = em.createQuery("SELECT m FROM Subject m "
                    + "WHERE m.code LIKE :code and m.calendar is null").setParameter("code", "%" + guess.toString() + "%");
        } else {
            q = em.createQuery("SELECT m FROM Subject m "
                    + "WHERE m.name LIKE :name and m.calendar is null").setParameter("name", "%" + guess + "%");
        }
        try {
            materias = q.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return materias;
    }

    @Override
    public List<IEntity> getList(EntityManager em) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Subject.class));
        Query q = em.createQuery(cq);
        return q.getResultList();
    }

    public List<Subject> getSubjectsByUser(String user, EntityManager em) {
        List<Subject> materias = new ArrayList<>();
        Student s = null;
        Calendar c = null;
        
        Query st = em.createQuery("SELECT s FROM Student s "
                + "WHERE s.username = :username").setParameter("username", user);
        Query cl = em.createQuery("SELECT c FROM Calendar c "
                + "WHERE c.student = :user");
        Query q = em.createQuery("SELECT m FROM Subject m "
                + "WHERE m.calendar = :cal");
        try {
            s = (Student) st.getSingleResult();
            cl.setParameter("user", s);
            c = (Calendar) cl.getSingleResult();
            q.setParameter("cal", c);
            materias = q.getResultList();
        } catch (NonUniqueResultException e) {
            s = (Student) st.getResultList().get(0);
            c = (Calendar) cl.getResultList().get(0);
        } 
        return materias;
        
    }

    public List<Subject> getSubjects(EntityManager em) {
        List<Subject> materias = new ArrayList<>();
        Query q = em.createQuery("SELECT m FROM Subject m "
                + "WHERE m.calendar is null");
        try {
            materias = q.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return materias;
    }
}
